﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EzDesk.Attendance.Model;
using System.Text;
using EzDesk.Utility.Web.Extensions;

namespace EzDesk.Attendance.MobileWeb.Extensions
{
    public static class AttendanceEvaluateInfoExtensions
    {
        private static string newLineTag = "<br />";

        public static string RenderToHtml(this AttendanceInfo info)
        {
            if (info == null)
            {
                return "<small>未考勤</small>";
            }
            else
            {
                return info.ActionTime.FormattedTime();
            }
        }

        /// <summary>
        /// 是否显示详细信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool HasDetailInfo(this AttendanceEvaluationInfo info)
        {
            return 
                info.OnDutyAction != null || 
                info.OffDutyAction != null || 
                !String.IsNullOrEmpty(info.GetRemark());
        }

        public static string GetRemark(this AttendanceEvaluationInfo info)
        {
            StringBuilder sb = new StringBuilder();

            if (info.CustomizedShiftWork != null)
            {
                if (info.CustomizedShiftWork.CustomizedShitWorkMiniute > 0)
                {
                    sb.Append("今日使用调休时间：");
                }
                else if(info.CustomizedShiftWork.CustomizedShitWorkMiniute < 0)
                {
                    sb.Append("今日获得调休：");
                }                
            }
            else
            {
                if (info.IsLate)
                {
                    sb.Append(
                        String.Format(
                            "<small>迟到{0}分钟</small>，需要用 {1} 小时 调休抵消本次迟到。" + newLineTag,
                            info.LateMiniutes.ToString(),
                            FormattedHourString(info.ReplacedLateHours)
                            )
                        );
                }
                if (info.IsEarlyLeave)
                {
                    sb.Append(
                        String.Format(
                            "<small>早退{0}分钟</small>，需要用 {1} 小时 调休抵消本次早退。" + newLineTag,
                            info.EarlyMiniutes.ToString(),
                            FormattedHourString(info.ReplacedEarlyHours)
                            )
                        );
                }
                if (info.IsExtraWork)
                {
                    sb.Append(
                        String.Format(
                            "<strong>加班 {0} 分钟</strong>，得到 {1} 小时调休" + newLineTag,
                            info.ExtraWorkTime.ToString(),
                            FormattedHourString(info.ReplacedExtraWorkTime)
                            )
                            );
                }
                if (info.IsLeaveWholeDay)
                {
                    sb.Append(
                        String.Format(
                            "全天 {0}" + newLineTag,
                            info.LeaveInfo.FirstLeave.LeaveName
                        )
                        );
                }
                if (info.IsFragmentaryAttendance)
                {
                    sb.Append(
                        String.Format(
                            "<small>今日未完整考勤!</small>" + newLineTag
                            )
                        );
                }
                if (info.IsAbsence)
                {
                    sb.Append(
                        String.Format(
                            "<small>今日未考勤!</small> 需要用 {0} 小时调休抵消本次旷工。" + newLineTag,
                            FormattedHourString(info.ShiftWorkHours)
                            )
                        );
                }
            }

            return sb.ToString();
        }

        public static string GetSimpleRemark(this AttendanceEvaluationInfo info)
        {
            StringBuilder sb = new StringBuilder();

            if (info.IsLate)
            {
                sb.Append(
                    String.Format(
                        "<small>迟到{0}分钟</small>" + newLineTag,
                        info.LateMiniutes.ToString(),
                        FormattedHourString(info.ReplacedLateHours)
                        )
                    );
            }
            if (info.IsEarlyLeave)
            {
                sb.Append(
                    String.Format(
                        "<small>早退{0}分钟</small>" + newLineTag,
                        info.EarlyMiniutes.ToString(),
                        FormattedHourString(info.ReplacedEarlyHours)
                        )
                    );
            }
            if (info.IsExtraWork)
            {
                sb.Append(
                    String.Format(
                        "<strong>加班 {0} 分钟</strong>" + newLineTag,
                        info.ExtraWorkTime.ToString(),
                        FormattedHourString(info.ReplacedExtraWorkTime)
                        )
                        );
            }
            if (info.IsLeaveWholeDay)
            {
                sb.Append(
                    String.Format(
                        "全天 {0}" + newLineTag,
                        info.LeaveInfo.FirstLeave.LeaveName
                    )
                    );
            }

            return sb.ToString();
        }

        public static string GetDutyActionTimeString(this AttendanceInfo info)
        {
            return info.ActionTime.FormattedTime();
        }

        public static string GetDateDescription(this DayInfo dayInfo)
        {
            string description = null;
            if (dayInfo.NeedToWork)
            {
                description = "工作日";
            }
            if (dayInfo.IsWeekEnd)
            {
                description = "周末";
            }
            if (dayInfo.HolidayInfo != null)
            {
                description = dayInfo.HolidayInfo.Name;
            }
            return description;
        }

        public static string GetActionTypeString(this DutyTypeEnum type)
        {
            string str = null;
            switch (type)
            {
                case DutyTypeEnum.OnDuty:
                    str = "上班";
                    break;
                case DutyTypeEnum.OffDuty:
                    str = "下班";
                    break;
            }
            return str;
        }

        private static string FormattedHourString(int hourCount)
        {
            string hourString = hourCount.ToString();
            hourString = hourString.Insert(hourString.Length - 1, ".");
            if (hourString.Length == 2)
            {
                hourString = "0" + hourString;
            }
            return hourString;
        }
    }
}
